#include "CSpecialItem.h"
#include "CGameApp.h"
//#include "CLaserWeapon.h"
#include "CWeapon.h"

extern float VIEW_WIDTH;
extern float VIEW_HEIGHT;


CSpecialItem::CSpecialItem()
:CEntity(),
TimeAlive(0.0f),
TimeUntilAlive(0.0f),
Waiting(false),
m_bIsItemAttached(false),
m_bUpdating(false),
m_pHost(NULL),
ShipSpeed(0)
{
	SetType( "CSpecialItem" );
}

CSpecialItem::~CSpecialItem()
{
	if( m_pHost )
	{
		m_pHost->SetSpeed( ShipSpeed );
		CPlayer* player = (CPlayer*)m_pHost;
		player->SetRateOfFire( 3 );
		m_pHost = NULL;
	}
}


void CSpecialItem::Render()
{
	CEntity::Render();
}

void CSpecialItem::Update()
{
	if( GetAlive() == false && Waiting == true )//TimeUntilAlive >= 0 )
	{
		TimeUntilAlive -= pParent->DeltaTime;//	decrease timeuntil alive
		if( TimeUntilAlive <= 0 )
		{
			SetWaiting( false );
			SetAlive( true );
			SetTimeAlive( 0 );// = 0;
		}
	}
	if( GetAlive() == true )
	{
		TimeAlive += pParent->DeltaTime;
		MovePosY( pParent->DeltaTime * GetSpeed() * GetVelY() ); 
		MovePosX( pParent->DeltaTime * GetSpeed() * GetVelX() );
		float edge = pParent->WorldEdge;
		if( GetPosY() < -edge || GetPosY() > edge
			|| GetPosX() < -edge || GetPosX() > edge )
		{ Kill(); }

		for( int i = 0; i < pParent->NumPlayers; i++ )
		{
			if( COLLISION( pParent->Player[i] , this ) )
			{
				pParent->Player[i]->AttachItem( this );
				//AttachToHost( pParent->Player[0] );
				pParent->DetachItem( this );
				//cout << this << endl;
				//CWeapon* wpn = new CLaserWeapon( 3 , 5 );
				//pParent->Player[0]->EquipPrimary( wpn );
				//Kill();
			}
		}
	}

	if( GetAlive() == false && GetEAlive() == false && Waiting == false )
	{
		SetUpdating( false );
	}

	CEntity::Update();
}

void CSpecialItem::UpdateAttached()
{
	//cout << "Specialitem updateattached " << endl;
	//this is where the special effects happen
	CEntity* Host = GetHost();
	//follow the host
	SetOldPos( GetPosX(), GetPosY() );
	SetPos( Host->GetPosX() , Host->GetPosY()+5 );
	if( GetAlive() )
	{
		//cout << "it's alive" << endl;
	}
	//SetAlive( true );

	//Host->SetSpeed( Host->GetSpeed() + 2 * pParent->DeltaTime );

	if( GetAlive() == false && GetEAlive() == false && Waiting == false )
	{
		SetUpdating( false );
	}
//cout << "Specialitem updateattached end of function" << endl;
	CEntity::Update();
}



void CSpecialItem::Load()
{
	if( pParent == NULL ) { cout << "CSpecialItem load failed, no game attached" << endl; return; }
	SetAlive(true);
	SetHealth( 100 );
	SetDamage( 0.0f );
	SetFaceAngle( 90.0f );
	SetLifeSpan( 100 );
	SetPos( pParent->Player[0]->GetPosX() , pParent->Player[0]->GetPosY() );
	SetOldPos( GetPosX(), GetPosY() );
	//SetVel( 0.0f, 1.0f );
	SetSpeed( 1.0f );
	SetRotateSpeed( 1.0f );//0.2f * (0.2f) );
	SetSize( 3.8f );
	SetCollisionSize( 3.8f );
	SetTexture( pParent->GhostTexture.texID );
	SetDamageTexture( pParent->EnemyDamageTexture.texID );
	SetExplosionTexture( pParent->ExplosionTexture );

	SetAlive( false );
	SetWaiting( true );
	SetTimeUntilAlive( 0 );/// float(rand()%20)/3.0f + 2.0f;//i*0.5f+1;//1.0f * (i+1);

	SetUpdating( true );

}


void CSpecialItem::TakeDamage( float damage)
{
	SetHealth( -1 );
	{
		Kill();
	}
}

void CSpecialItem::Kill()
{
	SetAlive( false );
	StartExplosion();
	SetSize( 3.8f );
}

void CSpecialItem::AttachToHost( CEntity* host )
{
	if( host == NULL ) { cout << "null host, can't attach item" << endl; return; }

	//SetIsItemAttached( true );
	//SetUpdating( true );
	m_pHost = host;
	SetLifeSpan( 50 );
	SetSize(10);
	if( host->GetType() == "CPlayer" )
	{
		CPlayer* player = (CPlayer*)host;
		//player->SetRateOfFire( 15 );
	}
	//ShipSpeed = host->GetSpeed();
	//host->SetSpeed( 15 );
	//host->AttachItem( this );

}

//void CSpecialItem::DisconnectFromHost()
//{
//	if( GetHost() == NULL ) { return; }
//	SetIsItemAttached( false );
//	SetUpdating( false );
//	//this function should be handled by the host
//}